package com.systemsjr.mpf.db;

import java.util.HashMap;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import com.systemsjr.mpf.expense.ExpenseType;

public class ExpenseTypeContentProvider extends MpfContentProvider {
	public static final String AUTHORITY = "com.systemsjr.mpf.db.ExpenseTypeContentProvider";
	private static final int EXPENSETYPES = 1;
	// URI MATCH OF A SPECIFIC CITIZEN QUERY
	private static final int ETCODE = 2;

	protected static UriMatcher sUriMatcher;
	protected static HashMap<String, String> projectionMap;
	@Override
	public boolean onCreate() {
		dbHelper = new MpfDbOpenHelper(this.getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {

		SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
		qb.setTables(ExpenseType.TABLE_NAME);

		switch (sUriMatcher.match(uri)) {
		case EXPENSETYPES:
			qb.setProjectionMap(projectionMap);
			break;
		case ETCODE:
			String code = uri.getPathSegments().get(ExpenseType.CODE_PATH_POSITION);
			qb.setProjectionMap(projectionMap);
			qb.appendWhere(ExpenseType.CODE + "=" + code);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor c = qb.query(db, projection, selection, selectionArgs, null,
				null, sortOrder);
		c.setNotificationUri(getContext().getContentResolver(), uri);

		return c;
	}

	@Override
	public String getType(Uri uri) {

		switch (sUriMatcher.match(uri)) {
		case EXPENSETYPES:
			return ExpenseType.CONTENT_TYPE;
		case ETCODE:
			return ExpenseType.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		
		if(sUriMatcher.match(uri) != EXPENSETYPES){
			throw new
			IllegalArgumentException("Unknown URI " + uri);
		}
		
		ContentValues contentValues;
		
		if(values != null){
			contentValues = new ContentValues(values);
		} else{
			contentValues = new ContentValues();
		}
		
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		
		long rowid = db.insert(ExpenseType.TABLE_NAME, null, contentValues);
		
		if(rowid > 0){
			Uri thisUri = ContentUris.withAppendedId(ExpenseType.CONTENT_URI, rowid);
			getContext().getContentResolver().notifyChange(thisUri, null);
			return thisUri;
		}
		throw new SQLException("Failed to insert row into " + uri);	
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case EXPENSETYPES:
			count = db.delete(ExpenseType.TABLE_NAME, selection, selectionArgs);
			break;
		case ETCODE:
			count = db.delete(ExpenseType.TABLE_NAME, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {

		SQLiteDatabase db = dbHelper.getWritableDatabase();
		int count;

		switch (sUriMatcher.match(uri)) {
		case EXPENSETYPES:
			count = db.update(ExpenseType.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		case ETCODE:
			
			count = db.update(ExpenseType.TABLE_NAME, values, selection,
					selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		getContext().getContentResolver().notifyChange(uri, null);
		return count;
	}

	static {

		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		sUriMatcher.addURI(AUTHORITY, "expenseType", EXPENSETYPES);
		sUriMatcher.addURI(AUTHORITY, "expenseType/#", ETCODE);
		
		// PROJECTION MAP USED FOR ROW ALIAS
		projectionMap = new HashMap<String, String>();
		projectionMap.put(ExpenseType.ID, ExpenseType.ID);
		projectionMap.put(ExpenseType.CODE, ExpenseType.CODE);
		projectionMap.put(ExpenseType.EXPENSE_TYPE, ExpenseType.EXPENSE_TYPE);
	}

}
